package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.mission.schedule.R;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.receiver.BootBroadcastReceiver;
import com.mission.schedule.service.LocalSynService;
import com.mission.schedule.utils.SharedPrefUtil;

/**
 * 数据同步上传对话框(转正式，数据修复使用)
 * Created by liwei on 2017/4/18.
 */

public class LocalSynServiceDialog extends Dialog {

    CallBack callBack;

    LocalSynServiceReciver localSynServiceReciver;

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    private ProgressBar c_pr, r_pr, n_pr, l_pr, p_pr;
    private ImageView c_im, r_im, n_im,l_im,p_im;
    //0 初始状态 ,1 成功 ,-1 失败
    private int cflag = 0, rflag = 0, nflag = 0, lflag = 0, pflag = 0;


    public LocalSynServiceDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;
        setCancelable(false);

        View view = LayoutInflater.from(context).inflate(R.layout.local_to_server_dialog, null);
        setContentView(view);


        c_pr = view.findViewById(R.id.c_pr);
        r_pr = view.findViewById(R.id.r_pr);
        n_pr = view.findViewById(R.id.n_pr);
        l_pr = view.findViewById(R.id.l_pr);
        p_pr = view.findViewById(R.id.p_pr);

        c_im = view.findViewById(R.id.c_im);
        r_im = view.findViewById(R.id.r_im);
        n_im = view.findViewById(R.id.n_im);
        l_im = view.findViewById(R.id.l_im);
        p_im = view.findViewById(R.id.p_im);

        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);

        localSynServiceReciver = new LocalSynServiceReciver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("localSynServiceReciver");
        context.registerReceiver(localSynServiceReciver, intentFilter);


        Intent LocalSyni = new Intent(context, LocalSynService.class);
        LocalSyni.setAction("LocalSynService");
        LocalSyni.setPackage(context.getPackageName());
        context.startService(LocalSyni);

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        params.alpha = 0.52f;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);// 设置生效
        window.setGravity(Gravity.CENTER);

    }

    class LocalSynServiceReciver extends BootBroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            super.onReceive(context, intent);
            if (intent != null) {
                if (intent.getStringExtra("cflag") != null) {
                    cflag = -1;
                    if (intent.getStringExtra("cflag").equals("cflag")) {
                        cflag = 1;
                        c_pr.setVisibility(View.GONE);
                        c_im.setVisibility(View.VISIBLE);
                    } else {
                        cflag = -1;
                        c_pr.setVisibility(View.GONE);
                        c_im.setVisibility(View.VISIBLE);
                    }
                    r_pr.setVisibility(View.VISIBLE);
                } else if (intent.getStringExtra("rflag") != null) {
                    c_pr.setVisibility(View.GONE);
                    c_im.setVisibility(View.VISIBLE);
                    if (cflag != 1) {
                        cflag = -1;
                    }
                    if (intent.getStringExtra("rflag").equals("rflag")) {
                        rflag = 1;
                        r_pr.setVisibility(View.GONE);
                        r_im.setVisibility(View.VISIBLE);
                    } else {
                        rflag = -1;
                        r_pr.setVisibility(View.GONE);
                        r_im.setVisibility(View.VISIBLE);
                    }
                    n_pr.setVisibility(View.VISIBLE);
                } else if (intent.getStringExtra("nflag") != null) {
                    r_pr.setVisibility(View.GONE);
                    r_im.setVisibility(View.VISIBLE);
                    if (rflag != 1) {
                        rflag = -1;
                    }
                    if (intent.getStringExtra("nflag").equals("nflag")) {
                        nflag = 1;
                        n_pr.setVisibility(View.GONE);
                        n_im.setVisibility(View.VISIBLE);
                    } else {
                        nflag = -1;
                        n_pr.setVisibility(View.GONE);
                        n_im.setVisibility(View.VISIBLE);
                    }
                    l_pr.setVisibility(View.VISIBLE);
                } else if (intent.getStringExtra("lflag") != null) {
                    n_pr.setVisibility(View.GONE);
                    n_im.setVisibility(View.VISIBLE);
                    if (nflag != 1) {
                        nflag = -1;
                    }
                    if (intent.getStringExtra("lflag").equals("lflag")) {
                        lflag = 1;
                        l_pr.setVisibility(View.GONE);
                        l_im.setVisibility(View.VISIBLE);
                    } else {
                        lflag = -1;
                        l_pr.setVisibility(View.GONE);
                        l_im.setVisibility(View.VISIBLE);
                    }
                    p_pr.setVisibility(View.VISIBLE);
                } else if (intent.getStringExtra("pflag") != null) {
                    l_pr.setVisibility(View.GONE);
                    l_im.setVisibility(View.VISIBLE);
                    if (lflag != 1) {
                        lflag = -1;
                    }
                    if (intent.getStringExtra("pflag").equals("pflag")) {
                        pflag = 1;
                        p_pr.setVisibility(View.GONE);
                        p_im.setVisibility(View.VISIBLE);
                    } else {
                        pflag = -1;
                        p_pr.setVisibility(View.GONE);
                        p_im.setVisibility(View.VISIBLE);
                    }
                    if (isShowing()) {
                        dismissDialog();
                    }
                }

                if (cflag == -1) {
                    c_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                } else if (rflag == -1) {
                    r_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                } else if (nflag == -1) {
                    n_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                } else if (lflag == -1) {
                    l_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                }else if (pflag == -1) {
                    p_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                }
            } else {
                dismissDialog();
            }
        }
    }

    private void dismissDialog(){
        dismiss();
        if (callBack != null) {
            callBack.dialogDismiss();
        }
        if (localSynServiceReciver != null){
            context.unregisterReceiver(localSynServiceReciver);
        }
        Toast.makeText(context,"数据同步完成",Toast.LENGTH_SHORT).show();
    }


    public interface CallBack{
        void dialogDismiss();
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
}
